package com.linkage.jz.bankruptcy.activity.RegistLogin;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.MainActivity;
import com.linkage.jz.bankruptcy.activity.Person.PersonSettingPhoneActivity;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.bean.LoginInfo;
import com.linkage.jz.bankruptcy.bean.TokenInfo;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.utils.SignUtil;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;
import com.linkage.seapeak.activity.register.BindAccountActivity;
import com.linkage.seapeak.activity.register.RegisterActivity;
import com.linkage.seapeak.base.BaseWhiteActivity;
import com.linkage.seapeak.pojo.QQAuthData;
import com.linkage.seapeak.pojo.WxAuthData;
import com.linkage.seapeak.utils.ToastUtil;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Random;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * A login screen that offers login via email/password.
 */
public class AccountLoginActivity extends BaseWhiteActivity implements OnClickListener {
    private static final String TAG = AccountLoginActivity.class.getSimpleName();

    private ShapeLoadingDialog shapeLoadingDialog;

    EditText et_username;
    EditText et_password;
    TextView getPwd, btn_login;
    CheckBox ck_remember;
    View ll_phonelogin, ll_register, wxlogin, qqlogin;

    public static void start(Context context) {
        Intent intent = new Intent(context, AccountLoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_login, "账号密码登录");
        initview();
        String user = SPUtils.getInstance(Consts.SPNAME).getString("loginUser");
        String pwd = SPUtils.getInstance(Consts.SPNAME).getString("loginPwd");
        if (!user.equalsIgnoreCase("")) {
            et_username.setText(user);
            et_password.setText(pwd);
            ck_remember.setChecked(true);
        }


        shapeLoadingDialog = new ShapeLoadingDialog(this);

        ck_remember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    if("".equalsIgnoreCase(et_username.getText().toString().trim()) ||
                            "".equalsIgnoreCase(et_password.getText().toString().trim())  )
                    {
                        ck_remember.setChecked(false);

                        noticeDialog = new PublicNoticeDialog(AccountLoginActivity.this,onClickListener,
                                5,"提示","请先填写用户名和密码",
                                "","确定");
                        noticeDialog.show();
                    }
                }else{

                }
            }
        });


        et_username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                showSave();
//                showCode();
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                showSave();
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
//        showCode();
        showSave();
    }





    private void showSave()
    {
        String str1 =  et_username.getText().toString().trim();
        String str2 =  et_password.getText().toString().trim();


        if(!str1.equalsIgnoreCase("") && !str2.equalsIgnoreCase("")
                )
        {

            btn_login.setClickable(true);
            btn_login.setBackgroundColor(getResources().getColor(R.color.btsel));
        }else{
            btn_login.setClickable(false);
            btn_login.setBackgroundColor(getResources().getColor(R.color.btnosel));
        }


    }
    private PublicNoticeDialog noticeDialog;
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.ll_close:
                    noticeDialog.dismiss();

                    break;

                case R.id.btn_chancel_notice:
//                   Boolean flag =  noticelxDialog.cb_agree.isChecked();
                    noticeDialog.dismiss();


                    break;

                case R.id.btn_save_notice:

                    noticeDialog.dismiss();
                    break;

            }
        }
    };

    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);
        ll_phonelogin = findViewById(R.id.ll_phonelogin);
        ll_register = findViewById(R.id.ll_register);
        getPwd = findViewById(R.id.getPwd);
        ck_remember = findViewById(R.id.ck_remember);
        wxlogin = findViewById(R.id.wx_login);
        qqlogin = findViewById(R.id.qq_login);

        btn_login.setOnClickListener(this);
        ll_phonelogin.setOnClickListener(this);
        ll_register.setOnClickListener(this);
        getPwd.setOnClickListener(this);
        wxlogin.setOnClickListener(this);
        qqlogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        String phone = "";
        String code = "";
        String pwd = "";
        switch (view.getId()) {
            case R.id.btn_login:
                //返回
//                MainActivity.start(this);
//                finish();
                phone = et_username.getText().toString();
                pwd = et_password.getText().toString();
                if ("".equalsIgnoreCase(phone)) {
                    showToast("手机号不能为空");
                    return;
                }
                if ("".equalsIgnoreCase(pwd)) {
                    showToast("密码不能为空");
                    return;
                }
                LoginUser(phone, code, pwd);
                break;
            case R.id.ll_phonelogin:
                PhoneLoginActivity.start(this);
                finish();
                break;
            case R.id.ll_register:
                RegisterActivity.start(this);
                finish();
                break;
            case R.id.getPwd:
                GetPwdOneActivity.start(this);
                break;
            case R.id.wx_login:
                authLogin(Wechat.NAME);
                break;
            case R.id.qq_login:
                authLogin(QQ.NAME);
                break;
            default:
                break;
        }


    }

    public void saveCid() {
        if (BankruptcyApp.tokeninfo == null || BankruptcyApp.tokeninfo.getToken() == null) {
            getToken(null);
            return;
        }

        shapeLoadingDialog.setLoadingText("提交信息中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();

//        LoginUser(mobile, code, pwd);
        String saveCid = SPUtils.getInstance(Consts.SPNAME).getString("saveCid", "");
        OkHttpClient okHttpClient = new OkHttpClient();
        HashMap<String, String> params = new HashMap<String, String>();
        Random random = new Random();
        String rom = random.nextLong() + "";
        String timestamp = System.currentTimeMillis() + "";

        RequestBody requestBody = new FormBody.Builder()
                .add("appid", "1000000001")
                .add("nonce", rom)
                .add("timestamp", timestamp)
                .add("token", BankruptcyApp.tokeninfo.getToken())
//                .add("token", "")
                .add("appVersion", "0.0.1")
                .add("cid", saveCid)
                .add("os", "0")
//                .add("osVersion", "123")
//                .add("model", "123")
//                .add("manufacturer", "123")
                .add("osVersion", Build.VERSION.SDK)
                .add("model", android.os.Build.MODEL)
                .add("manufacturer", android.os.Build.MANUFACTURER)
                .build();

        String url = Consts.WEBURL_SAVECID;
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                Log.d(TAG, "onFailure: " + e.getMessage());
                shapeLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.d(TAG, response.protocol() + " " +response.code() + " " + response.message());
                Headers headers = response.headers();


                shapeLoadingDialog.dismiss();
                String str = response.body().string();
                try {
                    JSONObject object = new JSONObject(str);
                    if (object.optInt("code") == 0) {
                        Gson gson = new Gson();
                        JSONObject data = object.getJSONObject("data");
                        String cid = data.optString("appInfoId");
//                        BankruptcyApp.tokeninfo = info;
//                        String saveCid = SPUtils.getInstance(Consts.SPNAME).getString("saveCid","");
//                        if("".equalsIgnoreCase(saveCid))
//                        {
                        SPUtils.getInstance(Consts.SPNAME).put("appInfoId", cid);

//                        }
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI

                            }

                        });
                    } else {

                    }

                    str = "";
                } catch (Exception ex) {

                    ex.printStackTrace();
                }
            }
        });


    }


    public void LoginUser(final String mobile, String code, final String pwd) {

        if (BankruptcyApp.tokeninfo == null || BankruptcyApp.tokeninfo.getToken() == null) {
//            Toast.makeText(AccountLoginActivity.this, "没有token", Toast.LENGTH_SHORT).show();
            getTokenMain(mobile, code, pwd);
            return;
        }
        String appInfoId = SPUtils.getInstance(Consts.SPNAME).getString("appInfoId", "");
        if ("".equalsIgnoreCase(appInfoId)) {
            saveCid();

            return;
        }
        Log.d(TAG, "LoginUser:  appInfoId " +appInfoId );
        shapeLoadingDialog.setLoadingText("加载中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();
        OkHttpClient okHttpClient = new OkHttpClient();
//        HashMap<String, String> params = new HashMap<String, String>();
        Random random = new Random();
        String rom = random.nextLong() + "";
        String timestamp = System.currentTimeMillis() + "";
        final Boolean flag = ck_remember.isChecked();
//        String saveCid = SPUtils.getInstance(Consts.SPNAME).getString("saveCid","");
        RequestBody requestBody = new FormBody.Builder()
                .add("appid", "1000000001")
                .add("nonce", rom)
                .add("timestamp", timestamp)
                .add("mobile", mobile)
                .add("token", BankruptcyApp.tokeninfo.getToken())
                .add("pwd", pwd)
                .add("smsCaptcha", code)
                .add("appInfoId", appInfoId)
                .add("loginType", "2")
                .build();

        String url = Consts.WEBURL_LOGIN;
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: " + e.getMessage());
                shapeLoadingDialog.dismiss();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        //更新UI
                        showToast("登录失败");
                    }

                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, response.protocol() + " " + response.code() + " " + response.message());
                Headers headers = response.headers();
                for (int i = 0; i < headers.size(); i++) {
                    Log.d(TAG, headers.name(i) + ":" + headers.value(i));
                }
                shapeLoadingDialog.dismiss();

                String str = response.body().string();

                try {
                    final JSONObject object = new JSONObject(str);
                    if (object.optInt("code") == 0) {
                        if (flag) {
                            SPUtils.getInstance(Consts.SPNAME).put("loginUser", mobile);
                            SPUtils.getInstance(Consts.SPNAME).put("loginPwd", pwd);
                        } else {
                            SPUtils.getInstance(Consts.SPNAME).put("loginUser", "");
                            SPUtils.getInstance(Consts.SPNAME).put("loginPwd", "");
                        }
                        Gson gson = new Gson();
                        JSONObject data = object.getJSONObject("data");

                        SPUtils.getInstance(Consts.SPNAME).put("userInfo", data.toString());

//                        SPUtils.getInstance(Consts.SPNAME).put("userInfobaseUrl",data.optString("baseUrl"));

                        String userToken = data.getString("userToken");

                        SPUtils.getInstance(Consts.SPNAME).put("userToken", userToken);


                         LoginInfo info = gson.fromJson(data.optString("userInfo"), LoginInfo.class);

                        SPUtils.getInstance(Consts.SPNAME).put("userId", info.getId());
                        BankruptcyApp.loginInfo = info;
                        BankruptcyApp.userToken = userToken;
                        MainActivity.start(AccountLoginActivity.this);
                        finish();
                    } else {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI
                                showToast("登录失败:"+object.optString("msg"));
                            }

                        });
                    }

                    str = "";
                } catch (Exception ex) {
                    ex.printStackTrace();
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            //更新UI
                            showToast("登录失败");
                        }

                    });
                }
            }
        });
    }

    public void getTokenMain(final String mobile, final String code, final String pwd) {

        shapeLoadingDialog.setLoadingText("获取token中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();

        OkHttpClient okHttpClient = new OkHttpClient();
        HashMap<String, String> params = new HashMap<String, String>();
        Random random = new Random();
        String rom = Math.abs(random.nextLong()) + "";
        String timestamp = System.currentTimeMillis() / 1000 + "";
        params.put("appid", Consts.APPID);
        params.put("nonce", rom);
        params.put("timestamp", timestamp);

        String sign = SignUtil.createSign(Consts.APPKEY, params);

        RequestBody requestBody = new FormBody.Builder()
                .add("appid", "1000000001")
                .add("nonce", rom)
                .add("timestamp", timestamp)
                .add("sign", sign)
                .build();

        String url = Consts.WEBURL_GETTOKEN;
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
//                Log.d(TAG, "onFailure: " + e.getMessage());
                String str = "";
                shapeLoadingDialog.dismiss();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        //更新UI
                        showToast("获取token网络异常");
                    }

                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.d(TAG, response.protocol() + " " +response.code() + " " + response.message());
                Headers headers = response.headers();

                shapeLoadingDialog.dismiss();

                String str = response.body().string();
//                Toast.makeText(AccountLoginActivity.this, "token返回String"+ str, Toast.LENGTH_SHORT).show();


                String msg = "";
                try {
                    final JSONObject object = new JSONObject(str);
                    if (object.optInt("code") == 0) {
                        Gson gson = new Gson();

                        TokenInfo info = gson.fromJson(object.optString("data"), TokenInfo.class);
                        BankruptcyApp.tokeninfo = info;
                        SPUtils.getInstance(Consts.SPNAME).put("tokeninfo", object.optString("data"));

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI
                                LoginUser(mobile, code, pwd);

                            }

                        });

                    } else {

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI
                                showToast("获取token网络异常");
                            }

                        });
                    }

                    str = "";
                } catch (final Exception ex) {
                    shapeLoadingDialog.dismiss();
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            //更新UI
                            showToast("获取token网络异常");
                        }

                    });
                    ex.printStackTrace();
                }
            }
        });
    }

    /**
     * 授权登录
     *
     * @param name
     */
    public void authLogin(final String name) {
        final Platform authPlatform = ShareSDK.getPlatform(name);
        ShareSDK.setActivity(this);//抖音登录适配安卓9.0
        //回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执行
        authPlatform.setPlatformActionListener(new PlatformActionListener() {

            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                arg2.printStackTrace();
                showToast("授权失败！");
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> hashMap) {
                //输出所有授权信息
               final  String authData = arg0.getDb().exportData();//取到所有数据
                Log.e("zhong", name+":name"+"授权 onComplete: " + authData);
                if (name.equals(QQ.NAME)) {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                    QQAuthData data = new Gson().fromJson(authData, QQAuthData.class);
                    qqLogin(data);
                        }

                    });
                } else {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            //更新UI
                            WxAuthData data = new Gson().fromJson(authData, WxAuthData.class);
                            wxLogin(data);

                        }

                    });
                }
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                showToast("取消授权！", ToastUtil.TOAST_TYPE.NOSHOW);
            }
        });
        //authorize与showUser单独调用一个即可
        authPlatform.authorize();//要功能不要数据，在监听oncomplete中不会返回用户数据
    }

    /**
     * QQ 登录
     *
     * @param data
     */
    public void qqLogin(final QQAuthData data) {
        String appInfoId = SPUtils.getInstance(Consts.SPNAME).getString("appInfoId", "");
        if ("".equalsIgnoreCase(appInfoId)) {
            saveCid();

            return;
        }
        Log.d(TAG, "qqLogin:  appInfoId " +appInfoId );
        shapeLoadingDialog.setLoadingText("加载中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();

        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = getTokenRequest()
                .add("openid", data.getUserID())
                .add("nickname", data.getNickname())
                .add("figureurl_qq", data.getIcon())
                .add("unionid", data.getUnionid())
                .add("gender", data.getGender().equals("0") ? "男" : "女")
                .add("pay_token", data.getPay_token())
                .add("secret", data.getSecret())
                .add("userID", data.getUserID())
                .add("expiresTime", data.getExpiresTime() + "")
                .add("token", data.getToken())
                .add("expiresIn", data.getExpiresIn() + "")
                .add("pfkey", data.getPfkey())
                .add("pf", data.getPf())
                .add("secretType", data.getSecretType())
                .add("iconQzone", data.getIconQzone())
                .add("icon", data.getIcon())
                .add("appInfoId",appInfoId)
                .build();

        Request request = new Request.Builder()
                .url(Consts.WEBURL_QQ_LOGIN)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                shapeLoadingDialog.dismiss();
                Log.d(TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, response.protocol() + " " + response.code() + " " + response.message());
                final String str = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            shapeLoadingDialog.dismiss();
                            JSONObject object = new JSONObject(str);
                            Log.i(TAG, "绑定的账号信息 onResponse: " + str);
                            JSONObject info = object.getJSONObject("data");

                            if (info != null) {
                                if (info.has("bind") && info.optInt("bind") == 0) {
                                    Log.i(TAG, "run: 已绑定QQ");
                                    LoginInfo userInfo = new Gson().fromJson(info.getString("userInfo"), LoginInfo.class);
//                                    Hawk.put(HawkKey.USERTOKEN, info.getString("userToken"));
//                                    Hawk.put(HawkKey.LOGINDETIAL, userInfo);
                                    BankruptcyApp.userToken = info.getString("userToken");

                                    BankruptcyApp.loginInfo = userInfo;
                                    SPUtils.getInstance(Consts.SPNAME).put("userInfo", object.getString("data"));
                                    SPUtils.getInstance(Consts.SPNAME).put("userToken", info.getString("userToken"));
                                    SPUtils.getInstance(Consts.SPNAME).put("userId", userInfo.getId());

                                    finish();
                                } else {
                                    Log.i(TAG, "run: 未绑定QQ");
                                    Intent intent = new Intent(AccountLoginActivity.this, BindAccountActivity.class);
                                    intent.putExtra("data", data);
                                    startActivity(intent);
                                    finish();
                                }

                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    /**
     * wx 登录
     *
     * @param data
     */
    public void wxLogin(final WxAuthData data) {
        String appInfoId = SPUtils.getInstance(Consts.SPNAME).getString("appInfoId", "");
        if ("".equalsIgnoreCase(appInfoId)) {
            saveCid();

            return;
        }

        Log.d(TAG, "wxLogin:  appInfoId " +appInfoId );
        shapeLoadingDialog.setLoadingText("加载中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();
        OkHttpClient okHttpClient = new OkHttpClient();
//        String appInfoId = SPUtils.getInstance(Consts.SPNAME).getString("appInfoId", "");
        RequestBody requestBody = getTokenRequest()
                .add("wxid",data.getOpenid())
                .add("openid", data.getOpenid())
                .add("nickname", data.getNickname())
                .add("figureurl_qq", data.getIcon())
                .add("unionid", data.getUnionid())
                .add("gender", data.getGender().equals("0") ? "男" : "女")
                .add("userID", data.getUserID())
                .add("expiresTime", data.getExpiresTime() + "")
                .add("token", data.getToken())
                .add("expiresIn", data.getExpiresIn() + "")
                .add("headimgurl", data.getIcon())
                .add("icon", data.getIcon())
                .add("appInfoId",appInfoId)
                .build();

        Request request = new Request.Builder()
                .url(Consts.WEBURL_WX_LOGIN)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                shapeLoadingDialog.dismiss();
                Log.d(TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG, response.protocol() + " " + response.code() + " " + response.message());
                final String str = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            shapeLoadingDialog.dismiss();
                            JSONObject object = new JSONObject(str);
                            Log.i(TAG, "绑定的账号信息 onResponse: " + str);

                            if(object.optInt("code") == 0) {
                                JSONObject info = object.getJSONObject("data");

                                if (info != null) {
                                    if (info.has("bind") && info.optInt("bind") == 0) {
                                        Log.i(TAG, "run: 已绑定WX");
                                        LoginInfo userInfo = new Gson().fromJson(info.getString("userInfo"), LoginInfo.class);
//                                    Hawk.put(HawkKey.USERTOKEN, info.getString("userToken"));
//                                    Hawk.put(HawkKey.LOGINDETIAL, userInfo);
                                        BankruptcyApp.userToken = info.getString("userToken");

                                        BankruptcyApp.loginInfo = userInfo;
                                        SPUtils.getInstance(Consts.SPNAME).put("userInfo", object.getString("data"));
                                        SPUtils.getInstance(Consts.SPNAME).put("userToken", info.getString("userToken"));
                                        SPUtils.getInstance(Consts.SPNAME).put("userId", userInfo.getId());

                                        finish();
                                    } else {
                                        Log.i(TAG, "run: 未绑定WX");
                                        Intent intent = new Intent(AccountLoginActivity.this, BindAccountActivity.class);
                                        intent.putExtra("data", data);
                                        startActivity(intent);
                                        finish();
                                    }
                                }
                            }else{
                                showToast("失败：" + object.optString("msg"));
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            }
        });
    }
}

